package com.github.ecbp.user.service.controller.front;

import com.github.ecbp.common.api.CommonResult;
import com.github.ecbp.common.data.builder.CommonPageBuilder;
import com.github.ecbp.common.data.controller.BaseController;
import com.github.ecbp.common.req.BaseIdDTO;
import com.github.ecbp.common.req.BaseIdListDTO;
import com.github.ecbp.common.req.SimpleIdListStatusParam;
import com.github.ecbp.user.service.dto.AdminQueryDto;
import com.github.ecbp.user.service.dto.AdminRolesDto;
import com.github.ecbp.user.service.request.AdminReq;
import com.github.ecbp.user.service.response.AdminRes;
import com.github.ecbp.user.service.response.RoleRes;
import com.github.ecbp.user.service.service.AdminSaveService;
import com.github.ecbp.user.service.service.AdminService;
import com.github.ecbp.user.service.service.RoleService;
import com.github.ecbp.user.service.vo.AdminPageVo;
import com.github.ecbp.user.service.vo.AdminVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;

import static com.github.ecbp.common.constant.CacheKeyConstant.PROJECT_NAME;

/**
 * <p>
 * 后台用户表 前端控制器
 * </p>
 *
 * @author zj2626
 * @since 2020-03-18
 */
@RequestMapping("/api/" + PROJECT_NAME + "/admin")
@Api(tags = "后台用户管理")
@RestController
public class AdminController extends BaseController {
    @Autowired
    protected AdminService service;
    @Autowired
    protected AdminSaveService adminSaveService;
    @Autowired
    private RoleService roleService;


    @ApiOperation(value = "新建用户(用户注册)", notes = "zhangj")
    @PostMapping(value = "/register")
    public CommonResult register(@Validated @RequestBody AdminReq param) {
        adminSaveService.registerUser(param);
        return CommonResult.succeed();
    }

    @ApiOperation(value = "分页列表-用户", notes = "zhangj", response = AdminPageVo.class)
    @PostMapping(value = "/selectPage")
    public CommonResult selectPage(@Validated @RequestBody AdminQueryDto param) {
        return CommonResult.succeed(CommonPageBuilder.build(service.page(param)));
    }

    @ApiOperation(value = "获取指定用户信息", notes = "zhangj", response = AdminVo.class)
    @PostMapping(value = "/info")
    public CommonResult getItem(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdDTO param) {
        return CommonResult.succeed(service.getById(param.getId()));
    }

    @ApiOperation(value = "重置用户密码", notes = "zhangj")
    @PostMapping(value = "/resetPassword")
    public CommonResult resetPassword(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdListDTO param) {
        adminSaveService.resetPassword(param);
        return CommonResult.succeed();
    }

    @ApiOperation(value = "批量修改帐号状态-启用/禁用", notes = "zhangj")
    @PostMapping(value = "/updateStatus")
    public CommonResult updateStatus(@Validated(BaseIdDTO.NotNullId.class) @RequestBody SimpleIdListStatusParam param) {
        adminSaveService.updateStatusById(param);
        return CommonResult.succeed();
    }

    @ApiOperation(value = "注销登录", notes = "zhangj")
    @PostMapping(value = "/doLogout")
    public CommonResult doLogout(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdListDTO param) {
        service.logout(param);
        return CommonResult.succeed();
    }

    @ApiOperation(value = "给用户分配角色", notes = "zhangj")
    @PostMapping(value = "/role/update")
    public CommonResult updateRole(@Validated(BaseIdDTO.NotNullId.class) @RequestBody AdminRolesDto param) {
        adminSaveService.updateRole(param);
        reloadPermission(param.getId());
        return CommonResult.succeed();
    }

    private void reloadPermission(Long id) {
        final AdminRes adminInfoById = service.getAdminInfoById(id);
        if (null != adminInfoById) {
            service.reloadUserPermission(Collections.singleton(adminInfoById.getLoginPhone()));
        }
    }

    @ApiOperation(value = "获取指定用户的角色", notes = "zhangj", response = RoleRes.class)
    @PostMapping(value = "/role")
    public CommonResult getRoleList(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdDTO param) {
        return CommonResult.succeed(roleService.getRoleListByAdminId(param.getId()));
    }
}
